package com.ymttest.business.service;

import com.shop2cn.iapi.member.parameter.*;
import com.shop2cn.iapi.member.service.*;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmch.buyer_invite_code;
import com.ymttest.database.model.godmch.buyer_invite_codeExample;
import com.ymttest.database.sqlwapper.godmch.buyer_invite_codeWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;

/**
 * 神器会员
 */
public class GodMemberService {

    /**
     * 设置会员备注
     * @param shopId
     * @param memberId
     * @param remark
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void SetMemberRemarkCall(int shopId,String memberId,String remark) throws ClientProtocolException, IOException {
    	MchSetMemberRemarkBean MchSetMemberRemarkBean=new MchSetMemberRemarkBean();
    	MchSetMemberRemarkCall 	MchSetMemberRemarkCall=new MchSetMemberRemarkCall();
    	MchSetMemberRemarkBean.setMemberId(memberId);
    	MchSetMemberRemarkBean.setShopId(shopId);
    	MchSetMemberRemarkBean.setRemark(remark);
    	MchSetMemberRemarkCall.setData(MchSetMemberRemarkBean);
    	MchSetMemberRemarkCall.callService();
    }
    /**
     * 快捷初始化CreateMemberBean
     * @return
     * @throws Exception 
     */
    public static CreateMemberBean initCreateMemberBean() throws Exception {
    	Integer shopid = GodmchManageCallService.CreateSellerMch();
		UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
		int userId = DGUserCallService.UserCreateCall(usercreateBean);
		CreateMemberBean creatememberBean=new CreateMemberBean(); 
		creatememberBean.setAccessSource(1);
		creatememberBean.setOpenId(usercreateBean.getOpenId());
		creatememberBean.setShopId(shopid);
		creatememberBean.setUserId(userId);
		return creatememberBean;
    }
    /**快捷初始化CreateMemberBean
     * @param shopid
     * @return
     * @throws Exception
     */
    public static CreateMemberBean initCreateMemberBean(Integer shopid) throws Exception {
		UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
		int userId = DGUserCallService.UserCreateCall(usercreateBean);
		CreateMemberBean creatememberBean=new CreateMemberBean(); 
		creatememberBean.setAccessSource(1);
		creatememberBean.setOpenId(usercreateBean.getOpenId());
		creatememberBean.setShopId(shopid);
		creatememberBean.setUserId(userId);
		return creatememberBean;
    }
    /**快捷初始化CreateMemberBean
     * @param shopid
     * @return
     * @throws Exception
     */
    public static CreateMemberBean initCreateMemberBean(Integer shopid,Integer userid,String openid) throws Exception {
		CreateMemberBean creatememberBean=new CreateMemberBean(); 
		creatememberBean.setAccessSource(1);
		creatememberBean.setOpenId(openid);
		creatememberBean.setShopId(shopid);
		creatememberBean.setUserId(userid);
		return creatememberBean;
    }
    /**
     * 更新中免身份证
     * @param shopId
     * @param userId
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static UpdateIdCardNoBean UpdateIdCardNo(Integer shopId,Integer userId) throws ClientProtocolException, IOException {
    	UpdateIdCardNoBean UpdateIdCardNoBean=new UpdateIdCardNoBean();
    	UpdateIdCardNoCall UpdateIdCardNoCall=new UpdateIdCardNoCall();
    	UpdateIdCardNoBean.setIdCardNo(TestDataManager.getCardId(18, 20));
    	UpdateIdCardNoBean.setIdName(TestDataManager.getChineseName());
    	UpdateIdCardNoBean.setShopId(shopId);
    	UpdateIdCardNoBean.setUserId(userId);
    	UpdateIdCardNoCall.setData(UpdateIdCardNoBean);
    	UpdateIdCardNoCall.callService();	
    	return UpdateIdCardNoBean;
    }
    /**
     * 查询会员信息
     * @param duserId
     * @return 
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static MemberInfoDto QueryMemberByDsid(Integer duserId) throws ClientProtocolException, IOException {
    	QueryMemberByDsIdBean querymemberbydsidBean=new QueryMemberByDsIdBean();
    	QueryMemberByDsIdCall querymemberbydsidCall=new QueryMemberByDsIdCall();
    	querymemberbydsidBean.setDsId(duserId);
		querymemberbydsidCall.setData(querymemberbydsidBean);
		querymemberbydsidCall.callService();			
		return querymemberbydsidCall.getMemberInfoDto();
    }
    /**
     * 创建新会员 返回会员号
     * @param creatememberBean
     * @return
     * @throws IOException 
     * @throws ClientProtocolException 
     */
    public static String creatememberCall(CreateMemberBean creatememberBean) throws ClientProtocolException, IOException {
    	CreateMemberCall creatememberCall=new CreateMemberCall();
    	creatememberCall.setData(creatememberBean);
    	creatememberCall.callService();
    	return creatememberCall.getCreateMemberResp().getMemberId();
    }
    /**
     * 绑定手机-没有客服id
     * @param shopid
     * @param userid
     * @param mobile
     * @throws IOException 
     * @throws ClientProtocolException 
     */
    public static void bindMobile(Integer shopid,Integer userid,String mobile) throws ClientProtocolException, IOException {
    	bindMobile(shopid, userid, mobile, null);
    }
    /**
     *  绑定手机
     * @param shopid
     * @param userid
     * @param mobile
     * @param csId
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void bindMobile(Integer shopid,Integer userid,String mobile,Integer csId) throws ClientProtocolException, IOException {
    	bindMobile(shopid, userid, mobile,csId,null);
    }
    /**
     *  绑定手机
     * @param shopid
     * @param userid
     * @param mobile
     * @param csId
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void bindMobile(Integer shopid,Integer userid,String mobile,Integer csId,Integer accessSource) throws ClientProtocolException, IOException {
    	BindMobileBean bindmobileBean=new BindMobileBean();
    	BindMobileCall bindmobileCall=new BindMobileCall();
    	bindmobileBean.setUserId(userid);
		bindmobileBean.setMobile(mobile);
		bindmobileBean.setShopId(shopid);
		bindmobileBean.setCsId(csId);
		bindmobileBean.setAccessSource(accessSource);
		bindmobileCall.setData(bindmobileBean);
		bindmobileCall.callService();	
    }
    /**
     * 获取用户信息
     * @param shopid
     * @param userid
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static BriefMemberInfoResp briefmemberinfoCall(Integer shopid,Integer userid) throws ClientProtocolException, IOException {
    	BriefMemberInfoBean briefmemberinfoBean=new BriefMemberInfoBean();
    	BriefMemberInfoCall briefmemberinfoCall=new BriefMemberInfoCall();
    	briefmemberinfoBean.setShopId(shopid);
		briefmemberinfoBean.setUserId(userid);
		briefmemberinfoCall.setData(briefmemberinfoBean);
		briefmemberinfoCall.callService();			
		return briefmemberinfoCall.getBriefMemberInfoResp();
    }
    
    /**
     * 采集会员信息
     * @param collectionmemberinfoBean
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void collectionmemberinfoCall(CollectionMemberInfoBean collectionmemberinfoBean) throws ClientProtocolException, IOException {
    	CollectionMemberInfoCall collectionmemberinfoCall=new CollectionMemberInfoCall();
    	collectionmemberinfoCall.setData(collectionmemberinfoBean);
		collectionmemberinfoCall.callService();		
    }
    /**
     * 商家给会员送积分
     * @param memberId
     * @param shopid
     * @param points
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void sendPoints(String memberId,Integer shopid,Integer points) throws ClientProtocolException, IOException {
    	MchSendPointsBean mchsendpointsBean=new MchSendPointsBean();
    	MchSendPointsCall mchsendpointsCall=new MchSendPointsCall();
    	mchsendpointsBean.setMemberId(memberId);
		mchsendpointsBean.setPoints(points);
		mchsendpointsBean.setRemark("atuoTest");
		mchsendpointsBean.setShopId(shopid);
		mchsendpointsCall.setData(mchsendpointsBean);
		mchsendpointsCall.callService();			
    }
    /**
     * 设置是否需要采集手机号
     * @param shopid
     * @param collectionMobile
     * @throws IOException 
     * @throws ClientProtocolException 
     */
    public static void setCollectionMobile(Integer shopid,boolean collectionMobile) throws ClientProtocolException, IOException {
    	MchSetCollectionMobileBean mchsetcollectionmobileBean=new MchSetCollectionMobileBean();
    	MchSetCollectionMobileCall mchsetcollectionmobileCall=new MchSetCollectionMobileCall();
    	mchsetcollectionmobileBean.setShopId(shopid);
		mchsetcollectionmobileBean.setCollectionMobile(collectionMobile);
		mchsetcollectionmobileCall.setData(mchsetcollectionmobileBean);
		mchsetcollectionmobileCall.callService();	
    }
    /**
     * 查询客服/用户的所有邀请码
     * @param csid
     * @return
     * @throws InterruptedException
     */
    public static List<buyer_invite_code> getBuyerInviteCode(Integer csid) throws InterruptedException {
    	buyer_invite_codeExample example=new buyer_invite_codeExample();
        example.createCriteria().andUserIdEqualTo(csid);
        buyer_invite_codeWapper codeMapper=new buyer_invite_codeWapper();
        List<buyer_invite_code> codes=codeMapper.selectByExample(example);      
        return codes;
    }
    /**
     * 邀请码会员制度设置绑定类型
     * @param shopid
     * @param bindtype  0 一次性 1永久
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void setBindTypeBean(Integer shopid,Integer bindtype) throws ClientProtocolException, IOException{
    	MchSetBindTypeBean mchsetbindtypeBean=new MchSetBindTypeBean();
    	MchSetBindTypeCall mchsetbindtypeCall=new MchSetBindTypeCall();
    	mchsetbindtypeBean.setBindType(bindtype);
		mchsetbindtypeBean.setShopId(shopid);
		mchsetbindtypeCall.setData(mchsetbindtypeBean);
		mchsetbindtypeCall.callService();		
    }
    /**
     * 开启邀请码会员制度
     * @param shopid
     * @param invitationSwitch
     * @param bindtype 0 一次性 1永久 该参数作废 请使用setBindTypeBean
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void setInvitationCodeCall(Integer shopid,Boolean invitationSwitch,Integer bindtype) throws ClientProtocolException, IOException {
    	MchSetInvitationCodeBean mchsetinvitationcodeBean=new MchSetInvitationCodeBean();
    	MchSetInvitationCodeCall mchsetinvitationcodeCall=new MchSetInvitationCodeCall();
    	mchsetinvitationcodeBean.setShopId(shopid);
		mchsetinvitationcodeBean.setBindType(bindtype);
		mchsetinvitationcodeBean.setInvitationSwitch(invitationSwitch);
		mchsetinvitationcodeCall.setData(mchsetinvitationcodeBean);
		mchsetinvitationcodeCall.callService();			
    }
    
    /**
     * 初始化默认积分配置bean
     * @param shopid
     * @return
     */
    public static MchSetOpenPointsRuleBean initMchSetOpenPointsRuleBean(Integer shopid) {
    	MchSetOpenPointsRuleBean mchsetopenpointsBean=new MchSetOpenPointsRuleBean();
//    	mchsetopenpointsBean.setOpenPoints(true);
		mchsetopenpointsBean.setShopId(shopid);
		List<PointsRuleDto> pointsRuleList=new ArrayList<PointsRuleDto>();
		//100000 - 下单送积分，110000 - 取消订单扣积分
		//200000 - 商家送积分，210000 - 商家扣积分
		PointsRuleDto prd=new PointsRuleDto();
		prd.setBizType(100000);
		prd.setPoints(5);
		prd.setThreshold("100");
		pointsRuleList.add(prd);
		
		PointsRuleDto prd2=new PointsRuleDto();
		prd2.setBizType(110000);
		prd2.setPoints(5);
		prd2.setThreshold("100");
		pointsRuleList.add(prd2);
		
		PointsRuleDto prd3=new PointsRuleDto();
		prd3.setBizType(200000);
		prd3.setPoints(1);
		prd3.setThreshold("1");
		pointsRuleList.add(prd3);
		
		PointsRuleDto prd4=new PointsRuleDto();
		prd4.setBizType(210000);
		prd4.setPoints(1);
		prd4.setThreshold("1");
		pointsRuleList.add(prd4);
		
		mchsetopenpointsBean.setPointsRuleList(pointsRuleList);
		return mchsetopenpointsBean;
    }
    /**
     * 开启(关闭)积分
     * @throws IOException 
     * @throws ClientProtocolException 
     */
    public static void mchSetOpenPoints(Integer shopId,boolean openPoints) throws ClientProtocolException, IOException {
    	MchSetOpenPointsBean mchsetopenpointsBean=new MchSetOpenPointsBean();
    	mchsetopenpointsBean.setOpenPoints(openPoints);
    	mchsetopenpointsBean.setShopId(shopId);
    	MchSetOpenPointsCall mchsetopenpointsCall=new MchSetOpenPointsCall();
    	mchsetopenpointsCall.setData(mchsetopenpointsBean);
		mchsetopenpointsCall.callService();			
    }
    public static void MchSetOpenPointsRule(MchSetOpenPointsRuleBean MchSetOpenPointsRuleBean) throws ClientProtocolException, IOException {
    	MchSetOpenPointsRuleCall MchSetOpenPointsRuleCall=new MchSetOpenPointsRuleCall();
    	MchSetOpenPointsRuleCall.setData(MchSetOpenPointsRuleBean);
    	MchSetOpenPointsRuleCall.callService();			
    }
    /**
     * 商家设置会员注册条款
     * @param shopid
     * @param regterm true  默认打勾 false 不打勾
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static void MchSeRegtermCall(Integer shopid,Boolean regterm) throws ClientProtocolException, IOException {
    	MchSeRegTermBean mchseregtermBean=new MchSeRegTermBean();
    	MchSeRegTermCall mchseregtermCall=new MchSeRegTermCall();
    	mchseregtermBean.setDefaultRegTerm(regterm);
		mchseregtermBean.setShopId(shopid);
		mchseregtermCall.setData(mchseregtermBean);
		mchseregtermCall.callService();	
    }
    /**
	 * 发送订单消息(增加积分)
	 * 
	 * @param buyerid
	 * @param sellerid
	 * @param orderid
	 * @param sqOrderRmbAmount
	 * @throws IOException
	 * @throws ClientProtocolException
	 */
	public static void processOrderPay(Integer buyerid, Integer sellerid, Long orderid, String sqOrderRmbAmount)
			throws ClientProtocolException, IOException {
		ProcessOrderPayBean processorderpayBean = new ProcessOrderPayBean();
		ProcessOrderPayCall processorderpayCall = new ProcessOrderPayCall();
		processorderpayBean.setBuyerId(buyerid);
		processorderpayBean.setSqOrderRmbAmount(sqOrderRmbAmount);
		processorderpayBean.setOrderId(orderid);
		processorderpayBean.setSellerId(sellerid);
		processorderpayBean.setPayTime(YMTDateUtil.getBeforeOrNextDay(0));
		processorderpayBean.setGoodsOwnerId(sellerid);
		processorderpayCall.setData(processorderpayBean);
		processorderpayCall.callService();
	}
	  /**
		 * 发送取消订单消息(扣积分)
		 * 
		 * @param buyerid
		 * @param sellerid
		 * @param orderid
		 * @throws IOException
		 * @throws ClientProtocolException
		 */
		public static void processOrderCancelPay(Integer buyerid, Integer sellerid, Long orderid,double refundAmountRmb)
				throws ClientProtocolException, IOException {
			PortionRefundBean processorderpayBean = new PortionRefundBean();
			PortionRefundCall processorderpayCall = new PortionRefundCall();
			processorderpayBean.setUserId(buyerid);
			processorderpayBean.setOrderId(orderid);
			processorderpayBean.setSellerId(sellerid);
			processorderpayBean.setRefundAmountRmb(new BigDecimal(refundAmountRmb));
			processorderpayBean.setBizNo(YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmss")+TestDataManager.getRandomNum(4));
			processorderpayCall.setData(processorderpayBean);
			
			processorderpayCall.callService();
		}
	/**
	 * 获取商家配置
	 * @param shopid
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static MchMemberConfigInfoCall getMchMemberConfigInfo(Integer shopid) throws ClientProtocolException, IOException {
		MchMemberConfigInfoBean mchmemberconfiginfoBean=new MchMemberConfigInfoBean();
		MchMemberConfigInfoCall mchmemberconfiginfoCall=new MchMemberConfigInfoCall();
		mchmemberconfiginfoBean.setShopId(shopid);
		mchmemberconfiginfoCall.setData(mchmemberconfiginfoBean);
		mchmemberconfiginfoCall.callService();
		return mchmemberconfiginfoCall;
	}
	public static void main(String[] args) throws Exception {
		Logger.debug(FormatUtil.beanToGSONString( GodMemberService.getBuyerInviteCode(500002398)));
		
	}
}
